package com.iap.admin.view.image.code
{
	import com.iap.admin.signal.ImageRequestSignal;
	import com.iap.common.translator.ImageTranslator;
	import com.iap.admin.view.image.ImageEditView;
	import com.iap.app.AppGlobal;
	import com.iap.app.StatusCode;
	import com.iap.app.model.ServiceResponse;
	import com.iap.app.translator.ServiceTranslator;
	import com.iap.app.util.FormUtils;
	import com.iap.app.util.logger.Log;
	import com.iap.common.model.Image;
	
	import flash.display.DisplayObject;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;
	import mx.events.ValidationResultEvent;
	import mx.validators.StringValidator;
	import mx.validators.Validator;
	
	import org.osflash.signals.Signal;
	
	import spark.components.Group;
	import com.iap.admin.view.code.EditViewCode;
	
	public class ImageEditViewCode extends EditViewCode
	{
		public static const CANCEL_NAME:String = "ImageCreate.Cancel";
		public static const SAVE_NAME:String = "ImageCreate.Save";
		public static const DELETE_NAME:String = "ImageCreate.Delete";

		public var onSubmit:ImageRequestSignal = new ImageRequestSignal();
		public var onComplete:Signal = new Signal(Image);
		
		private var view:ImageEditView;
		
		protected var file:FileReference;

		public function ImageEditViewCode()
		{
			super();
		}

		protected function initView(view:ImageEditView):void
		{
			this.view = view;
			addListener();
			initValidators();
			resetFocus();
			onReady.dispatch("ready");
		}
		
		private function initValidators():void
		{
		}
		
		private function addListener():void
		{
		}
		
		private function removeListener():void
		{
		}
		
		private function resetFocus():void
		{
		}
		
		protected function submit():void 
		{
			model ||= new Image();
			onSubmit.dispatch(model, ImageRequestSignal.SAVE);
		}
		
		protected function remove():void 
		{
			onSubmit.dispatch(model, ImageRequestSignal.DELETE);
		}

		protected function browseClick(event:MouseEvent):void
		{
			if(file==null)
			{
				file = new FileReference();
				addListeners(file);
			}
			file.browse(getTypes());
		}

		protected function getTypes():Array
		{
			var allTypes:Array = new Array(new FileFilter
				(
					"Images (*.jpg; *.jpeg; *.gif; *.png)",
					"*.jpg; *.jpeg; *.gif; *.png; *.tiff; *.tif;"
				));
			return allTypes;
		}

		private function addListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadDataComplete);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(Event.SELECT, selectHandler);
		}
		
		private function removeListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadDataComplete);
			dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatcher.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.removeEventListener(Event.SELECT, selectHandler);
		}

		private function selectHandler(event:Event):void
		{
			setFileName(file.name);
			sendFile();
		}

		private function setFileName(fileName:String):void
		{
			view.fileForm.text = fileName;
		}

		public function sendFile():void
		{
			var request:URLRequest = new URLRequest();
			request.url = AppGlobal.FileUploadEndPoint;
			file.upload(request,"file");
		}

		private function uploadDataComplete(evt:DataEvent):void
		{
			removeListeners((evt.target as FileReference));
			var serviceRespones:ServiceResponse = ServiceTranslator.translate(evt.data);
			var result:Image = ImageTranslator.translate(serviceRespones.result) as Image;
			view.progressBar.visible = false;
			onComplete.dispatch(result);
		}

		private function ioErrorHandler(event:IOErrorEvent):void
		{
			removeListeners(file);
			Log.Info("FileUpload.ioErrorHandler: " + event + " url="+AppGlobal.FileUploadEndPoint);
			view.progressBar.visible = false;
		}

		private function progressHandler(event:ProgressEvent):void
		{
			view.progressBar.visible = true;
		}
		
		override public function fill(model:Object):void
		{
			super.fill(model);
			view.fileForm.enabled = false;
			view.fileBrowseForm.enabled = false;
			view.setCurrentState("edit");
			view.imageLoader.load((model as Image).thumbnail); 
		}
	}
}